(function(window, undifined) {
	/*
	 * 2014/05/15 update
	 * alert, comfirm 使用JSON传送参数
	 */

	"user strict";

	var modal = function() {};

	// 动作模块
	modal.prototype.actions = function(config) {
		var oAction = document.createElement("div");
		oAction.setAttribute("class", "actions-modal");
		var html = "",
			_this = this;
		for (var i = 0; i < config.length; i++) {
			for (var j = 0; j < config[i].length; j++) {
				0 === j && (html += "<div class='actions-modal-group'>")
				var h = config[i][j],
					itemClass = (h.label ? "actions-modal-label" : "actions-modal-button");
				html += "<span class='" + itemClass + "'>" + h.text + "</span>";
				j === config[i].length - 1 && (html += "</div>");
			}
		}

		oAction.innerHTML = html;
		document.body.appendChild(oAction);
		var oActionHeight = getComputedStyle(oAction, null)["height"];
		oAction.style.marginTop = oActionHeight;

		var modalGroups = oAction.querySelectorAll(".actions-modal-group");
		[].forEach.call(modalGroups, function(itemGroups, group) {
			var itemGroup = itemGroups.children;
			[].forEach.call(itemGroup, function(itemButton, button) {
				if (itemButton.classList.contains("actions-modal-button")) {
					config[group][button].click && itemButton.addEventListener("touchend", config[group][button].click, false);
					config[group][button].close && itemButton.addEventListener("touchend", function() {
						_this.closeModal(oAction);
					}, false);
				}
			});
		});
		this.openModal(oAction);
	};

	// 弹出模块
	modal.prototype.alert = function(configs) {
		var defaults = {
			"text": "",
			"title": "提示",
			"buttons": [{
				"text": "确认",
				"close": true,
				"click": configs.callback
			}]
		}
		for (var i in defaults) {
			configs[i] || (configs[i] = defaults[i]);
		};
		return this.modalAll(configs);
	};

	// 确认模块
	modal.prototype.confirm = function(configs) {
		var defaults = {
			"text": "",
			"title": "提示",
			"buttons": [{
				"text": "取消",
				"close": true
			}, {
				"text": "确认",
				"close": true,
				"click": configs.callback
			}]
		}
		for (var i in defaults) {
			configs[i] || (configs[i] = defaults[i]);
		};
		return this.modalAll(configs);
	};

	// 弹出，确认引用模块
	modal.prototype.modalAll = function(configs) {
		var oModal = document.createElement("div");
		oModal.setAttribute("class", "modal");
		var buttonsHTML = "";
		var modalTemplate = "<div class='modal-inner'><div class='modal-title'>{{title}}</div><div class='modal-text'>{{text}}</div></div><div class='modal-buttons'>{{buttons}}</div>";
		if (configs.buttons && configs.buttons.length > 0) {
			for (var i = 0; i < configs.buttons.length; i++) {
				buttonsHTML += "<span class='modal-button'>" + configs.buttons[i].text + "</span>";
			}
		}
		var html = modalTemplate.replace(/{{title}}/g, configs.title).replace(/{{text}}/g, configs.text).replace(/{{buttons}}/g, buttonsHTML);
		oModal.innerHTML = html;
		document.body.appendChild(oModal);

		//添加事件
		var modalButtons = oModal.querySelectorAll(".modal-button"),
			_this = this;
		[].forEach.call(modalButtons, function(itemButton, index) {
			configs.buttons[index].click && itemButton.addEventListener("touchend", configs.buttons[index].click, false);
			configs.buttons[index].close && itemButton.addEventListener("touchend", function() {
				_this.closeModal(oModal);
			}, false);
		});

		this.openModal(oModal);
	};

	modal.prototype.openModal = function(elem) {
		var div = document.createElement("div");
		div.setAttribute("class", "modal-overlay");
		document.body.appendChild(div);

		setTimeout(function() {
			elem.classList.add("modal-in");
			div.classList.add("modal-overlay-visible");
		}, 0);
	};

	modal.prototype.closeModal = function(elem) {
		elem.classList.remove("modal-in");
		elem.classList.add("modal-out");
		var overlay = document.querySelector(".modal-overlay");
		overlay.classList.contains("modal-overlay-visible") && overlay.classList.remove("modal-overlay-visible");

		setTimeout(function() {
			document.body.removeChild(elem);
			document.body.removeChild(overlay);
			///document.querySelector ( ".modal-overlay" ).remove();
			// 手机上面不支持 remove() 方法。微信浏览器，android 4.0
		}, 500);
	};

	window.modal = new modal();

})(window)